package com.huiyeji.dynamic.core.parser;

import com.huiyeji.dynamic.core.DynamicContext;
import com.huiyeji.dynamic.core.DynamicRegex;
import com.huiyeji.dynamic.core.DynamicStatement;
import com.huiyeji.dynamic.core.RegexContainer;
import com.huiyeji.dynamic.core.exception.DynamicStatementParseException;
import com.huiyeji.dynamic.core.provider.ContextParamProvider;
import com.huiyeji.dynamic.core.provider.IntegerParamProvider;
import com.huiyeji.dynamic.core.provider.ParamProvider;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class IncrementDecrementParser implements DynamicParser{

    public  static final String REGEX = "^\\s*"+ DynamicRegex.PARAM_CONTEXT_OBJECT_NAME.origin()+"\\s*(\\+\\+|\\-\\-)\\s*$";


    @Override
    public DynamicStatement parse(DynamicContext context, String statement) {
        String paramName = "";
        String methodName = "";
        if (statement.contains("++")){
            paramName = statement.replace("++","").trim();
            methodName = "add";
        } else if (statement.contains("--")){
            paramName = statement.replace("--","").trim();
            methodName = "sub";
        } else {
            throw new DynamicStatementParseException("格式错误");
        }

        List<ParamProvider> paramProviders = new ArrayList<ParamProvider>();
        paramProviders.add(new ContextParamProvider(paramName));
        paramProviders.add(new IntegerParamProvider("1"));

        return new DynamicStatement(statement,true,paramName,null,"integer",methodName,null,paramProviders);
    }

    @Override
    public RegexContainer ruleInitialize() {
        return new RegexContainer(Collections.singletonList(
                REGEX
        ));
    }

    @Override
    public int getOrder() {
        return 1000;
    }
}
